package lumingweihua.future.cn.lumingweihua.ui.order;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhusx.core.adapter.Lib_BaseRecyclerAdapter;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Densitys;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.ui.BaseAppFragment;
import lumingweihua.future.cn.lumingweihua.ui.order.domain.OrderData;

/**
 * Author       zhusx
 * Email        327270607@qq.com
 * Created      2018/3/15 15:34
 */

public class OrderListFragment extends BaseAppFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    int index;
    Lib_BaseRecyclerAdapter<OrderData.Bean> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_order_list, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        init();
        LoadData<OrderData> loadData = new LoadData<>(LoadData.Api.货主订单, this);
        loadData._setOnLoadingListener(new LoadingHelper<OrderData>(recyclerView, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<OrderData> data) {
                if (data.getData() != null) {
                    List<OrderData.Bean> list = null;
                    switch (index) {
                        case 0:
                            list = data.getData().quanbu;
                            break;
                        case 1:
                            list = data.getData().daizhuanghuo;
                            break;
                        case 2:
                            list = data.getData().yunshuzhong;
                            break;
                        case 3:
                            list = data.getData().jiesu;
                            break;
                    }
                    if (list == null) {
                        list = new ArrayList<>();
                    }
                    adapter._setItemToUpdate(list);
                }
            }
        });
        loadData._loadData();
    }

    private void init() {
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(0, _Densitys.dip2px(view.getContext(), 10), 0, 0);
            }
        });
        recyclerView.setAdapter(adapter = new Lib_BaseRecyclerAdapter<OrderData.Bean>() {
            @Override
            protected void __bindViewHolder(_ViewHolder viewHolder, int i, final OrderData.Bean s) {
                viewHolder.setText(R.id.tv_num, "货单号:" + s.good_num);
                viewHolder.setText(R.id.tv_count, s.car_num + "辆车运输");
                viewHolder.setText(R.id.tv_fromName, s.loading);
                viewHolder.setText(R.id.tv_fromCity, s.loading_address);
                viewHolder.setText(R.id.tv_toName, s.unload);
                viewHolder.setText(R.id.tv_toCity, s.unload_address);
                viewHolder.setText(R.id.tv_type, s.type);
                viewHolder.setText(R.id.tv_weight, "共" + s.weight + "吨");

                viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(v.getContext(), OrderDetailActivity.class).putExtra(_EXTRA_String_ID, s.good_num));
                    }
                });
            }

            @Override
            protected int __getLayoutResource(int i) {
                return R.layout.item_list_order;
            }
        });
    }

    public void setData(int index) {
        this.index = index;
    }
}
